package com.acme.serialization.impl;

import com.acme.serialization.ObjectSerializer;
import com.acme.serialization.SerializationException;
import com.thoughtworks.xstream.XStream;

import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author Gregory Sobko
 */
public class XstreamObjectSerializer<T> implements ObjectSerializer<T> {
    private final XStream xstream = new XStream();

    @Override
    public String getProtocolName() {
        return "xml";  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public T deserialize(InputStream inputStream) throws SerializationException {
        return (T)xstream.fromXML(inputStream);
    }

    @Override
    public void serialize(T object, OutputStream outputStream) throws SerializationException {
        xstream.toXML(object, outputStream);
    }
}
